//
//  APIService+Register.m
//  JiZhuiAn
//
//  Created by roger lin on 16/6/19.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "APIService+Register.h"

@implementation APIService (Register)

- (RACSignal *)registerWithPhoneNumber:(NSString *)phoneNumber
                              username:(NSString *)username
                              password:(NSString *)password
                               SMSCode:(NSString *)SMSCode
{
    BmobUser *newUser = [[BmobUser alloc]init];
    [newUser setUsername:phoneNumber];
    [newUser setPassword:password];
    newUser.mobilePhoneNumber = phoneNumber;
    [newUser setObject:username forKey:@"nickName"];
    [newUser setObject:@(0) forKey:@"hadSpineTested"];
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [newUser signUpOrLoginInbackgroundWithSMSCode:SMSCode block:^(BOOL isSuccessful, NSError *error) {
            if (error) {
                [subscriber sendError:error];
            }
            else{
                [subscriber sendNext:@(isSuccessful)];
                [subscriber sendCompleted];
                
            }
        }];
        return nil;
    }];
}


- (RACSignal *)getCaptchaWithPhoneNumber:(NSString *)phoneNumber
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {

        [BmobSMS requestSMSCodeInBackgroundWithPhoneNumber:phoneNumber andTemplate:@"register" resultBlock:^(int number, NSError *error) {
            if (error) {
                [subscriber sendError:error];
            }
            else{
                [subscriber sendNext:@(number)];
                [subscriber sendCompleted];
            }
        }];
        return nil;
    }];
}
@end
